package com.tjdr.his.drugstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjdr.his.domain.entity.DrugInfo;
import com.tjdr.his.domain.entity.Employee;
import com.tjdr.his.domain.entity.GiveBackRecords;
import com.tjdr.his.domain.entity.Register;
import com.tjdr.his.domain.mapper.IDrugInfoMapper;
import com.tjdr.his.domain.mapper.IEmployeeMapper;
import com.tjdr.his.domain.mapper.IGiveBackRecordsMapper;
import com.tjdr.his.domain.mapper.IRegisterMapper;
import com.tjdr.his.drugstore.service.IGiveBackRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/18 16:43
 * @DESCRIBER
 */
@Service
@Slf4j
public class IGiveBackRecordsServiceImpl implements IGiveBackRecordsService {
    @Autowired
    private IGiveBackRecordsMapper mapper;
    @Autowired
    private IRegisterMapper registerMapper;
    @Autowired
    private IDrugInfoMapper drugInfoMapper;
    @Autowired
    private IEmployeeMapper employeeMapper;
    @Override
    public IPage getGiveBackRecordsByProperty(GiveBackRecords records,int pageSize,int currentPage) {

        //select * from give_back_records t
        // where t.date='2020/1/1' and id like '%aa%' and operator_state ='发药'
        LambdaQueryWrapper<GiveBackRecords> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(records.getDate()!=null){
            SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = simpleDateFormat.format(records.getDate());
            lambdaQueryWrapper.like(GiveBackRecords::getDate,dateStr);
        }

        if (records.getId() != null) {
            lambdaQueryWrapper.like(GiveBackRecords::getId, records.getId());
        }
        if (records.getOperatorState() != null && !"".equals(records.getOperatorState().trim())) {
            lambdaQueryWrapper.eq(GiveBackRecords::getOperatorState, records.getOperatorState());
        }
        IPage page = new Page();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        lambdaQueryWrapper.orderByDesc(GiveBackRecords::getDate);
        log.debug("page:{},{}",page.getCurrent(),page.getSize());
        IPage resultPage = mapper.selectPage(page,lambdaQueryWrapper);

       List<GiveBackRecords> list = resultPage.getRecords();
       log.debug("list:{}",list);
       for(GiveBackRecords g:list){
           //根据患者编号 查找患者信息
           Register register = registerMapper.selectById(g.getRegisterId());
           // 根据药品编号 查找药品信息
           DrugInfo drugInfo =drugInfoMapper.selectById(g.getDrugId());
           //根据医生编号 查找医生信息
           Employee employee = employeeMapper.selectById(g.getEmployeeId());
           g.setRegister(register);
           g.setDrugInfo(drugInfo);
           g.setEmployee(employee);
       }

       resultPage.setRecords(list);

       return resultPage;




    }

    @Override
    public List getGiveBackRecords() {
        List<GiveBackRecords> list = mapper.selectRecords();

        return list;
    }


    @Override
    public int saveGiveBackRecords(GiveBackRecords record) {
       return  mapper.insert(record);

    }
}
